// pages/addcustomer/addcustomer.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:0,
      mindate:Math.round((new Date() - 365*86400*1000)),
      projects:[],
      customer:false,
      account:false,
      // invoice_no:"",
      invoice_date:"",
      currency:"",
      language:"",
      desc:"",
      agente:"",
      payment_ddl:"",
      showSelectInvoiceDate:false,
      showSelectPaymentDDL:false,
      pay_method:"",
      discount:100.00,
      freight:0.00,
      freighted:0.00,
      tax_refund_act:"",
      tax_refund_act_no:"",
      box_num:"",
      weight:0,
      net_weight:0,
      trade_regulations:"",
      business_index:0,
      advance_amount:0.00,
      autosize:true,
      wsjg:0.00,
      hszj:0.00,
      s:0.00,
      fpze:0.00
  },
  onSelectInvoiceDate(){
    this.setData({
      showSelectInvoiceDate:true
    })
  },
  onInvoiceDateClose(){
    this.setData({
      showSelectInvoiceDate:false
    })
  },
  onInvoiceDateConfirm(e){
    this.setData({
      invoice_date:e.detail.toLocaleString('zh', { hour12: false }),
      showSelectInvoiceDate:false
    })
  },
  onSelectPaymentDDL(){
    this.setData({
      showSelectPaymentDDL:true
    })
  },
  onPaymentDDLClose(){
    this.setData({
      showSelectPaymentDDL:false
    })
  },
  onPaymentDDLConfirm(e){
    console.log(e)
    this.setData({
      payment_ddl:e.detail.toLocaleString('zh', { hour12: false }),
      showSelectPaymentDDL:false
    })
  },
  selectProject(){
    var ids = [];
    console.log(ids)
    for(var i=0;i<this.data.projects.length;i++){
      ids.push(this.data.projects[i].id)
    }
    var that = this;
    request.requestAjax('invoice/computedAmount',{ids:ids},"计算中",function(res){
      console.log(res.data)
      console.log(res.data.hszj*that.data.discount/100)
      if(res.status==200){
          that.setData({
            wsjg: res.data.wsjg,
            hszj:res.data.hszj,
            s:res.data.s,
            fpze:res.data.hszj*that.data.discount/100
          })
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

    },function(res){
      wx.showToast({
        title: '计算失败',
        icon:"none"
      })
    });
    
  },
  selectScope(events){
      console.log(events)
      this.setData({
        business_index:events.detail.value,
        trade_regulations:this.data.scope_data[events.detail.value]
      })
      this.onClose()
  },
  showPopup() {
    this.setData({ showPop: true });
  },

  onClose() {
    console.log(this.data.scope_data)
    this.setData({ showPop: false });
  },
  bindKeyInputVant(event){
    // console.log(event);
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
    // console.log(this.data)
  },
  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  delProject(e){
    console.log(e)
    var that = this;
    let projects = that.data.projects;
    projects.splice(e.currentTarget.dataset.index,1);
    that.setData({
      projects
    })
  },
  submit(){
    // console.log(that.data.projects);
  
    var that = this;
    // var data = {
    //   name:this.data.name,
    //   email:this.data.email,
    //   phone:this.data.phone,
    //   countryT:this.data.countryT,
    //   religion:this.data.religion,
    //   duty_no:this.data.duty_no,
    //   address:this.data.address,
    //   desc:this.data.desc,
    //   buse
    // }
    var that = this;
    var data = that.data;
    console.log(that.data)
    request.requestAjax('invoice/add',data,"加载中",function(res){
      if(res.status==200){
        wx.showToast({
          title: '保存成功',
          icon:"success"
        })
        setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            // var customer = res.data;
            // prevPage.setData({
            //   customer:customer
            // })
            wx.showToast({
              title: "新增成功",
              icon:"success"
            })
            if(Reflect.has(prevPage,"getList")){
              prevPage.getList();
            }
       
            wx.navigateBack();
        },1000);
          return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

  },function(res){
    wx.showToast({
      title: '提交失败',
      icon:"none"
    })
  });
  },
  getBusinessScope(){
    
    var that = this;
    request.requestAjax('invoice/getInternationalTradeRegulations',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              scope_data:res.data
            })
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  delPic(e){
    console.log(e)
    var that = this;
    let fileList = that.data.fileList;
    fileList.splice(e.detail.index,1);
    that.setData({
      fileList
    })
  },
  showPic(e){
    console.log(e)
    return true;
  },
  afterRead(event) {
    var that = this;
    const { file } = event.detail;
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: "图片上传中",
    })
      

    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://www.jufupen.online/api/upload/image', // 仅为示例，非真实的接口地址
      filePath: file.url,
      header: {
        'Authori-zation':"Bearer "+wx.getStorageSync('token'),
      },
      name: 'file',
      formData: { },
      success(res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        // 上传完成需要更新 fileList
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url: res.data.url });
        that.setData({ fileList });
      },
      fail(){
        wx.showToast({
          title: '上传失败',
          icon:"error"
        })
        wx.hideLoading()
        wx.hideNavigationBarLoading()
      }
    });
  },
  getInfo(){
    var that = this;
    request.requestAjax('invoice/info',{id:this.data.id},"加载中",function(res){
        if(res.status==200){
            that.setData({
              projects:res.data.form.projects,
              customer:res.data.form.customer,
              account:res.data.form.account,
              // invoice_no:res.data.form.invoice_no,
              invoice_date:res.data.form.invoice_date,
              currency:res.data.form.currency,
              language:res.data.form.language,
              desc:res.data.form.desc,
              agente:res.data.form.agente,
              payment_ddl:res.data.form.payment_ddl,
              showSelectInvoiceDate:res.data.form.showSelectInvoiceDate,
              showSelectPaymentDDL:res.data.form.showSelectPaymentDDL,
              pay_method:res.data.form.pay_method,
              discount:res.data.form.discount,
              freight:res.data.form.freight,
              freighted:res.data.form.freighted,
              tax_refund_act:res.data.form.tax_refund_act,
              tax_refund_act_no:res.data.form.tax_refund_act_no,
              box_num:res.data.form.box_num,
              weight:res.data.form.weight,
              net_weight:res.data.form.net_weight,
              trade_regulations:res.data.form.trade_regulations,
              wsjg:res.data.form.wsjg,
              hszj:res.data.form.hszj,
              advance_amount:res.data.form.advance_amount,
              s:res.data.form.s,
              fpze:res.data.form.fpze,
          });
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBusinessScope();
    if(options.id!=undefined){
      this.setData({id:options.id})
      this.getInfo();
    }
  },
  resetComputedTotal(e){
    this.setData({
      fpze:this.data.hszj*this.data.discount/100
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})